﻿/*
 *  ChgDoc CMS - GNU General Public License, version 3 (GPL-3.0)
 *  Copyright (C)  2012. Ian Quigley
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using ChgDoc.Entity;

namespace ChgDoc.Edit
{
	[AdminPage( "List logins", "Admin" )]
	public partial class LoginList : BasePage
	{
        protected override IEnumerable<FormRule> GetFormRules()
        {
            return new List<FormRuleMapping<Login>>()
		                {
		                    new FormRuleMapping<Login>( txtEmail, "Email", FormRuleCondition.NotNull, (s,v) => s.Email = (string)v),
                            new FormRuleMapping<Login>( txtPassword, "Password", FormRuleCondition.NotNull, (s,v) => s.EncPassword = Login.Encode( (string)v )),
		                };
        }

		protected void Page_Load( object sender, EventArgs e )
		{
			if (IsPostBack)
				return;

			UpdateDisplay();
		}

		private void UpdateDisplay( )
		{
			grdList.DataSource = ChgDoc.Entity.Login.ReadAll();
			grdList.DataBind();
		}

		protected void grdList_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			long id = long.Parse( e.Item.Cells[ 0 ].Text );

			switch (e.CommandName)
			{
				case "Delete":
					ChgDoc.Entity.Login.Delete( id );
					UpdateDisplay();
					break;
			}
		}

        protected void btnSave_click( object sender, EventArgs e )
        {
            ChgDoc.Entity.Login model = new ChgDoc.Entity.Login() ;

            if (!PopulateModelFromControls<Login>( model ))
                return;
                        
            model.Save();

            Account account = new Account()
            {
                DisplayName = model.Email
            };
            account.Save();

            Authentication auth = new Authentication()
            {
                AccountId = account.Id,
                Mechanism = "site",
                MechanismsUniqueId = model.Id.ToString()
            };
            auth.Save();

            Role user = Role.ForCode("User");
            if (user == null)
            {
                user = new Role() { Code = "User", Description = "User" };
                user.Save();
            }

            AccountRole accountRole = new AccountRole()
            {
                AccountId = account.Id,
                RoleId = user.Id
            };
            accountRole.Save();

            UpdateDisplay();
        }
	}

}